local skynet = require "skynet"
local socket = require "socket"
require "skynet.manager"	-- import skynet.register
require "math"
require "public"

local json = require "cjson"
local ErrNo = require "common/ErrNo"
local ROOM_PLAERY_NUM = 4

CMD = {}
local rooms = {}


--大厅加入房间
function CMD.join_room_hall(data)
	if not data.room_id then 
		return ErrNo.UNKNOW_ERROR,nil
	end
	local room_id = math.floor(data.room_id)
	
	local room_info = 1
	
	if not room_info then 
		return ErrNo.UNKNOW_ERROR,nil
	end
	local room_data = { 
		room_id 	= room_id, 
		rule_type 	= room_info.rule_type,
		game_type	= 2,
		dizhu		= room_info.integral,
		room_type 	= 2,	
	}
	if rooms[room_id] == nil then 
		local room_id, room_fd = CMD.create_room(room_data)
		return room_id, room_fd
	end
	return room_id,rooms[room_id].room_fd
end

function CMD.create_room(data)
	local room_id = math.floor(data.room_id) 
	if not room_id then 
		return nil,nil -- 房间存在
	end
	if rooms[room_id] ~= nil then
		return nil,nil -- 房间存在
	end
	
	room_fd = skynet.newservice("yfmj_room")

    rooms[room_id] = {}
    rooms[room_id].room_fd = room_fd
    rooms[room_id].count = 1
    rooms[room_id].optionType = data.optionType
	if not data.dizhu then 
		data.dizhu = 1
	end
	if not data.room_type then 
		data.room_type = 1 -- 1私房，2.大@厅房间
	end
	if not data.total_ju or data.total_ju ~= 12  then 
		data.total_ju = 8
	end
 
	room_is_double = math.floor( string.sub(data.optionType,2,2) )
	room_is_piao = math.floor( string.sub(data.optionType,3,3) )
	if room_is_double ~= 1 then
    	room_is_double = 0
	end
	if room_is_piao ~= 1 then
    	room_is_piao = 0
	end 
	if not data.room_odds  then 
		data.room_odds = 1
	end
    skynet.call( room_fd, "lua", "start", room_id, data.ruleType,  data.optionType, data.total_ju, data.friend_id,room_is_double,room_is_piao,data.room_odds)
	data.room_is_double = room_is_double
	data.room_is_piao = room_is_piao
    return room_id,room_fd
end

function CMD.join_room(id)
	-- 房间状态
	local room_id = math.floor(id)
	if rooms[room_id] == nil then
		return ErrNo.ROOM_NOT_EXISTS,nil -- 房间不存在
	end
	if rooms[room_id].count >= ROOM_PLAERY_NUM then
		return ErrNo.ROOM_FULL,nil
	end

	local is_gps = 0
	if tonumber(string.sub(rooms[room_id].optionType,4,4)) ~= nil then
		is_gps = math.floor( string.sub(rooms[room_id].optionType,4,4) )
	end 
	return room_id,rooms[room_id].room_fd,is_gps
end


function CMD.join_room_again(room_id,uid)
	local room_id = math.floor(room_id)
	if rooms[room_id] == nil then
		return ErrNo.ROOM_NOT_EXISTS,nil -- 房间不存在
	end 
	return room_id,rooms[room_id].room_fd
end


function CMD.get_rooms(room_id)
	local room_id = math.floor(room_id)
	if rooms[room_id] == nil then
		return nil -- 房间不存在
	end 
	return rooms[room_id].room_fd
end

function CMD.room_status(id,data)
    -- skynet.error("room_status id: ",id,"count: ",data.count)
	local id = math.floor(id)
    rooms[id].count = data.count 
end

function CMD.destory(id) 
	if id then 
		local room_id = math.floor(id)
		skynet.call("MG_SAVE", "lua", "clean_room_redis", room_id)
		rooms[id] = nil
	else
		-- skynet.error("id had destory")
	end
end

skynet.start(function()
	skynet.dispatch("lua", function(_,_, command, ...)
		local f = CMD[command]
		skynet.ret(skynet.pack(f(...)))
	end)
	skynet.register "YFMJ_HALL"
end)

